// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.internal.hibernate;

import lichen.hibernate.SessionManaged;

import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * 实现管理的hibernate session.
 * @author jcai
 * @version $Revision: 225 $
 * @since 0.0.3
 */
public class SessionManagedImpl implements SessionManaged {
	private SessionFactory _sessionFactory;
	private Session _session;
	private boolean _participate;
	public SessionManagedImpl(SessionFactory factory){
		_sessionFactory = factory;
		_participate = false;
		initSession();
	}
	// 初始化session对象
	private void initSession() {
		// single session mode
		if (TransactionSynchronizationManager.hasResource(_sessionFactory)) {
			// Do not modify the Session: just set the participate flag.
			_participate = true;
		}
		else {
			Session session = getSession(_sessionFactory);
			_session=session;
			TransactionSynchronizationManager.bindResource(_sessionFactory, new SessionHolder(session));
		}
		_session = ((SessionHolder) TransactionSynchronizationManager.getResource(_sessionFactory)).getSession();
		
		
	}
	/**
	 * @see lichen.hibernate.SessionManaged#getSession()
	 * @see lichen.render.RenderModule#buildOpenSessionInThreadFilter(org.apache.tapestry5.services.RequestGlobals, SessionManaged, String)
	 * @since 0.0.3
	 */
	public Session getSession() {
		return _session;
	}
	protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
		Session session = SessionFactoryUtils.getSession(sessionFactory, true);
		FlushMode flushMode = getFlushMode();
		if (flushMode != null) {
			session.setFlushMode(flushMode);
		}
		return session;
	}
	private FlushMode getFlushMode() {
		return FlushMode.MANUAL;
	}

	protected void closeSession(Session session, SessionFactory sessionFactory) {
		SessionFactoryUtils.closeSession(session);
	}
	/**
	 * 
	 * @see org.apache.tapestry5.ioc.services.ThreadCleanupListener#threadDidCleanup()
	 * @since 0.0.3
	 */
	public void threadDidCleanup() {
		if (!_participate) {
			
			// single session mode
			SessionHolder sessionHolder =
					(SessionHolder) TransactionSynchronizationManager.unbindResource(_sessionFactory);
			
			closeSession(sessionHolder.getSession(), _sessionFactory);
			
		}
		
	}
}
